//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package ciicos.point.controller;

import ciicos.base.ActionResult;
import ciicos.base.UserInfo;
import ciicos.base.vo.PageListVO;
import ciicos.base.vo.PaginationVO;
import ciicos.exception.DataException;
import ciicos.iview.service.IviewService;
import ciicos.permission.entity.UserEntity;
import ciicos.point.entity.PointSyncEntity;
import ciicos.point.entity.PointUpdateRecordEntity;
import ciicos.point.model.pointupdaterecord.PointUpdateRecordForm;
import ciicos.point.model.pointupdaterecord.PointUpdateRecordInfoVO;
import ciicos.point.model.pointupdaterecord.PointUpdateRecordListVO;
import ciicos.point.model.pointupdaterecord.PointUpdateRecordPagination;
import ciicos.point.result.ScadaEntity;
import ciicos.point.result.ScadaResult;
import ciicos.point.service.PointSyncService;
import ciicos.point.service.PointUpdateRecordService;
import ciicos.util.DateUtil;
import ciicos.util.GeneraterSwapUtil;
import ciicos.util.JsonUtil;
import ciicos.util.RandomUtil;
import ciicos.util.StringUtil;
import ciicos.util.UserProvider;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(
    tags = {"点位下发记录"},
    value = "pointManage"
)
@RequestMapping({"/api/pointManage/PointUpdateRecord"})
public class PointUpdateRecordController {
    private static final Logger log = LoggerFactory.getLogger(PointUpdateRecordController.class);
    @Autowired
    private GeneraterSwapUtil generaterSwapUtil;
    @Autowired
    private UserProvider userProvider;
    @Autowired
    private PointUpdateRecordService pointUpdateRecordService;
    @Autowired
    private PointSyncService pointSyncService;
    @Autowired
    private IviewService iviewService;

    @ApiOperation("获取列表")
    @PostMapping({"/getList"})
    public ActionResult list(@RequestBody PointUpdateRecordPagination pointUpdateRecordPagination) throws IOException {
        List<PointUpdateRecordEntity> list = this.pointUpdateRecordService.getList(pointUpdateRecordPagination);
        List<PointUpdateRecordListVO> listVO = JsonUtil.getJsonToList(list, PointUpdateRecordListVO.class);

        for(PointUpdateRecordListVO pointUpdateRecordVO : listVO) {
            pointUpdateRecordVO.setSource_name(this.generaterSwapUtil.getDicName(pointUpdateRecordVO.getSource(), "466867814284656645"));
            pointUpdateRecordVO.setPointnmae_name(pointUpdateRecordVO.getPointnmae());
            pointUpdateRecordVO.setPointdesc_name(pointUpdateRecordVO.getPointdesc());
            pointUpdateRecordVO.setPointoldvalue_name(pointUpdateRecordVO.getPointoldvalue());
            pointUpdateRecordVO.setPointvalue_name(pointUpdateRecordVO.getPointvalue());
            pointUpdateRecordVO.setCreatoruserid_name(this.generaterSwapUtil.userSelectValue(pointUpdateRecordVO.getCreatoruserid()));
            pointUpdateRecordVO.setCreatortime_name(pointUpdateRecordVO.getCreatortime());
        }

        PageListVO vo = new PageListVO();
        vo.setList(listVO);
        PaginationVO page = (PaginationVO)JsonUtil.getJsonToBean(pointUpdateRecordPagination, PaginationVO.class);
        vo.setPagination(page);
        return ActionResult.success(vo);
    }

    @PostMapping
    @Transactional
    @ApiOperation("创建")
    public ActionResult create(@RequestBody @Valid PointUpdateRecordForm pointUpdateRecordForm) throws DataException {
        String b = this.pointUpdateRecordService.checkForm(pointUpdateRecordForm, 0);
        if (StringUtil.isNotEmpty(b)) {
            return ActionResult.fail(b + "不能重复");
        } else {
            String mainId = RandomUtil.uuId();
            UserInfo userInfo = this.userProvider.get();
            UserEntity userEntity = this.generaterSwapUtil.getUser(userInfo.getUserId());
            pointUpdateRecordForm.setCreatoruserid(userInfo.getUserId());
            pointUpdateRecordForm.setCreatortime(DateUtil.getNow());
            PointUpdateRecordEntity entity = (PointUpdateRecordEntity)JsonUtil.getJsonToBean(pointUpdateRecordForm, PointUpdateRecordEntity.class);
            entity.setId(mainId);
            this.pointUpdateRecordService.save(entity);
            return ActionResult.success("创建成功");
        }
    }

    @DeleteMapping({"/batchRemove/{ids}"})
    @Transactional
    @ApiOperation("批量删除")
    public ActionResult batchRemove(@PathVariable("ids") String ids) {
        String[] idList = ids.split(",");
        int i = 0;

        for(String allId : idList) {
            this.delete(allId);
            ++i;
        }

        return i == 0 ? ActionResult.fail("删除失败") : ActionResult.success("删除成功");
    }

    @ApiOperation("信息")
    @GetMapping({"/{id}"})
    public ActionResult<PointUpdateRecordInfoVO> info(@PathVariable("id") String id) {
        PointUpdateRecordEntity entity = this.pointUpdateRecordService.getInfo(id);
        PointUpdateRecordInfoVO vo = (PointUpdateRecordInfoVO)JsonUtil.getJsonToBean(entity, PointUpdateRecordInfoVO.class);
        vo.setCreatoruserid(this.generaterSwapUtil.userSelectValue(vo.getCreatoruserid()));
        if (vo.getCreatortime() != null) {
            vo.setCreatortime(vo.getCreatortime());
        }

        return ActionResult.success(vo);
    }

    @ApiOperation("表单信息(详情页)")
    @GetMapping({"/detail/{id}"})
    public ActionResult<PointUpdateRecordInfoVO> detailInfo(@PathVariable("id") String id) {
        PointUpdateRecordEntity entity = this.pointUpdateRecordService.getInfo(id);
        PointUpdateRecordInfoVO vo = (PointUpdateRecordInfoVO)JsonUtil.getJsonToBean(entity, PointUpdateRecordInfoVO.class);
        vo.setSource(this.generaterSwapUtil.getDicName(vo.getSource(), "466867814284656645"));
        vo.setPointnmae(vo.getPointnmae());
        vo.setPointdesc(vo.getPointdesc());
        vo.setPointoldvalue(vo.getPointoldvalue());
        vo.setPointvalue(vo.getPointvalue());
        vo.setCreatoruserid(this.generaterSwapUtil.userSelectValue(vo.getCreatoruserid()));
        vo.setCreatortime(vo.getCreatortime());
        return ActionResult.success(vo);
    }

    @PutMapping({"/{id}"})
    @Transactional
    @ApiOperation("更新")
    public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid PointUpdateRecordForm pointUpdateRecordForm) throws DataException {
        String b = this.pointUpdateRecordService.checkForm(pointUpdateRecordForm, 1);
        if (StringUtil.isNotEmpty(b)) {
            return ActionResult.fail(b + "不能重复");
        } else {
            PointUpdateRecordEntity entity = this.pointUpdateRecordService.getInfo(id);
            if (entity != null) {
                PointUpdateRecordEntity subentity = (PointUpdateRecordEntity)JsonUtil.getJsonToBean(pointUpdateRecordForm, PointUpdateRecordEntity.class);
                subentity.setCreatoruserid(entity.getCreatoruserid());
                subentity.setCreatortime(entity.getCreatortime());
                boolean b1 = this.pointUpdateRecordService.updateById(subentity);
                return !b1 ? ActionResult.fail("当前表单原数据已被调整，请重新进入该页面编辑并提交数据") : ActionResult.success("更新成功");
            } else {
                return ActionResult.fail("更新失败，数据不存在");
            }
        }
    }

    @ApiOperation("删除")
    @DeleteMapping({"/{id}"})
    @Transactional
    public ActionResult delete(@PathVariable("id") String id) {
        PointUpdateRecordEntity entity = this.pointUpdateRecordService.getInfo(id);
        if (entity != null) {
            this.pointUpdateRecordService.delete(entity);
        }

        return ActionResult.success("删除成功");
    }

    @ApiOperation("下发")
    @PostMapping({"/updateValue"})
    @Transactional
    public ActionResult updateValue(@RequestBody Map<String, Object> map) {
        log.info("点位下发 接口参数: {}", JsonUtil.getObjectToString(map));
        String tag = (String)map.get("tag");
        String updateValue = (String)map.get("updateValue");
        String source = (String)map.get("source");
        if (!StringUtil.isEmpty(tag) && !StringUtil.isEmpty(updateValue) && !StringUtil.isEmpty(source)) {
            PointSyncEntity byCode = this.pointSyncService.getByCode(tag);
            Map<String, Object> params = new HashMap();
            List<ScadaEntity> itmeList = new ArrayList();
            ScadaEntity entityParam = new ScadaEntity();
            if (byCode.getType().equals("16")) {
                entityParam.setName(tag + ".TEXT");
            } else {
                entityParam.setName(tag + ".PV");
            }

            entityParam.setVal(updateValue);
            itmeList.add(entityParam);
            params.put("items", itmeList);
            String realdata = this.iviewService.realdata((String)null, JsonUtil.getObjectToString(params));
            ScadaResult scadaResultUpdate = (ScadaResult)JsonUtil.getJsonToBean(realdata, ScadaResult.class);
            if (scadaResultUpdate.getCode() != 0) {
                return ActionResult.fail("SCADA接口下发失败，请检查点位");
            } else {
                PointUpdateRecordEntity entity = new PointUpdateRecordEntity();
                UserInfo userInfo = this.userProvider.get();
                entity.setSource(source);
                entity.setCreatoruserid(userInfo.getUserId());
                entity.setCreatortime(DateUtil.getNowDate());
                entity.setPointnmae(entityParam.getName());
                entity.setPointdesc(StringUtil.isNotEmpty(byCode.getName()) ? byCode.getName() : "");
                entity.setPointvalue(updateValue);
                this.pointUpdateRecordService.save(entity);
                return ActionResult.success();
            }
        } else {
            return ActionResult.fail("SCADA接口下发失败，参数不完整");
        }
    }

    @ApiOperation("下发记录-流程图")
    @PostMapping({"/updateValueByView"})
    @Transactional
    public ActionResult updateValueByView(@RequestBody Map<String, Object> map) {
        log.info("点位下发-流程图 接口参数: {}", JsonUtil.getObjectToString(map));
        String tag = (String)map.get("tag");
        String updateValue = (String)map.get("updateValue");
        String source = (String)map.get("source");
        if (!StringUtil.isEmpty(tag) && !StringUtil.isEmpty(updateValue) && !StringUtil.isEmpty(source)) {
            PointSyncEntity byCode = this.pointSyncService.getByCode(tag);
            PointUpdateRecordEntity entity = new PointUpdateRecordEntity();
            UserInfo userInfo = this.userProvider.get();
            entity.setSource(source);
            entity.setCreatoruserid(userInfo.getUserId());
            entity.setCreatortime(DateUtil.getNowDate());
            entity.setPointnmae(tag);
            entity.setPointdesc(StringUtil.isNotEmpty(byCode.getName()) ? byCode.getName() : "");
            entity.setPointoldvalue("");
            entity.setPointvalue(updateValue);
            this.pointUpdateRecordService.save(entity);
            return ActionResult.success();
        } else {
            return ActionResult.fail("流程图-SCADA接口下发失败，参数不完整");
        }
    }
}
